from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# 初始化 Chrome 浏览器
driver = webdriver.Chrome()

try:
    #1 打开 BOSS 直聘页面
    url = 'https://www.zhipin.com/web/geek/job?query=java&city=101290100'
    driver.get(url)

    #2 等待页面加载完成
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, 'job-list-box'))
    )

    #3 打印初始页面内容
    elements = driver.find_elements(By.CLASS_NAME, 'job-list-box')
    print("初始页面职位信息：")
    for element in elements:
        print(element.text)

    #4 模拟刷新页面
    driver.refresh()

    #5 等待页面刷新完成
    WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.CLASS_NAME, 'job-list-box'))
    )

    #6 刷新后再次爬取
    elements_after_refresh = driver.find_elements(By.CLASS_NAME, 'job-list-box')
    print("刷新后页面职位信息：")
    for element in elements_after_refresh:
        print(element.text)

finally:
    # 关闭浏览器
    driver.quit()